package com.qingxin.sys.service;

import java.util.Date;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.SysPermission;
import com.qingxin.sys.entity.SysRole;
import com.qingxin.sys.entity.SysRoleRights;
import com.qingxin.sys.mvc.SysError;

/**
 * 角色权限
 * @author Administrator
 *
 */
@Service
public class SysRoleRightsService extends BaseService<SysRoleRights, String> {
	
	
	/**
	 * 保存指定角色的权限
	 * @param roleid
	 * @param perids
	 * @param loginUser
	 * @return
	 */
	@CacheEvict(value="userRightsCache", allEntries = true)
	public SysError saveRoleRights(String roleid, String[] perids, LoginUser loginUser){
		evictCache("userRightsCache", null);
		//删除现有权限
		deleteRoleRights(roleid);
		
		//保存新设置的权限
		if(perids==null || perids.length==0){
			return null;
		}
		
		Date now = new Date();
		String create_by = loginUser.getLogin_name();
		for(String perid : perids){
			SysRoleRights rights = new SysRoleRights(new SysRole(roleid), new SysPermission(perid), create_by, now);
			this.save(rights);
		}
		return null;
	}
	
	
	/**
	 * 删除指定角色的所有权限
	 * @param roleid
	 * @return
	 */
	@CacheEvict(value="userRightsCache", allEntries = true)
	public SysError deleteRoleRights(String roleid){
		evictCache("userRightsCache", null);
		deleteByHql("delete from SysRoleRights r where r.sysRole.roleid=?", roleid);
		return null;
	}
	
	
}
